package com.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.domain.AllCart;
import com.domain.Goods;
import com.domain.Order;
import com.domain.OrderTitle;
import com.util.JdbcUtils;

/**
 * 
 * 功能描述：订单操作数据库类
 * 
 * Version： 1.0
 * 
 * date： 2016年6月26日 上午10:03:34
 * 
 * @author：XiaoBingBy
 *
 */
public class daoOrder {

	/**
	 * 产生一个新订单，操作数据库方法 把订单插入数据库
	 * @param uid
	 * @param username
	 * @param allCatInfo
	 * @param date 
	 */
	public void createOrder(String uid, String username, List<AllCart> allCatInfo, String date) {
		QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
		for (AllCart allCart : allCatInfo) {
			String sql = "INSERT INTO `order` (`order_id`, `username`, `goods_id`, `goods_name`, `goods_pic`, `goods_price`, `cart_quantity`, `order_state`, `order_date`) "
					+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
			try {
				queryRunner.update(sql, new Object[]{uid, username, allCart.getGoods_id(), allCart.getName(), allCart.getPic(), allCart.getPrice(), allCart.getNumber(), "发货", "2016-06-26 11:34:46"});
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 插入一个订单
	 * @param uuid
	 * @param username
	 * @param goodsInfo
	 */
	public void createOnOrder(String uuid, String username, Goods goodsInfo) {
		QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
		
		String sql = "INSERT INTO `order` (`order_id`, `username`, `goods_id`, `goods_name`, `goods_pic`, `goods_price`, `cart_quantity`, `order_state`, `order_date`) "
				+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
		try {
			queryRunner.update(sql, new Object[]{uuid, username, goodsInfo.getGoods_id(), goodsInfo.getGoods_dename(), goodsInfo.getGoods_pic(), goodsInfo.getGoods_price(), 1, "待处理", "2016-06-26 11:34:46"});
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取订单号 订单时间 订单数量 订单 状态 
	 * @return 
	 */
	public List<OrderTitle> selectOrderId(String username) {
		QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
		String sql = "SELECT `order`.order_id, `order`.cart_quantity, `order`.order_state, `order`.order_date FROM `order` WHERE `order`.username = ? GROUP BY `order`.order_id";
		try {
			List<OrderTitle> orders = queryRunner.query(sql, new BeanListHandler<>(OrderTitle.class), username);
			return orders;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 以购物车订单号查询商品信息
	 * @param order_id
	 * @return
	 */
	public List<Order> selectGoodsId(String order_id) {
		QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
		String sql = "SELECT * FROM `order` WHERE `order`.order_id = ?";
		try {
			List<Order> OrderInfo = queryRunner.query(sql, new BeanListHandler<>(Order.class), order_id);
			return OrderInfo;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

}
